﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VipCo.Entities;
using VipCo.Data;
using System.Data;
using System.Threading;

public partial class TinTucND : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["Id"] != null)
            {
                H_CatalogId.Value = Request.QueryString["id"].ToString();
                View_TinTuc(Convert.ToInt32(H_CatalogId.Value));

                Pager1.CurrentIndex = 1;
                HienThiBinhLuan(Pager1.CurrentIndex);
            }
        }
    }

    protected void View_TinTuc(int IdTinTuc)
    {
        TinTuc entTinTuc = DataRepository.TinTucProvider.GetById(IdTinTuc);
        try
        {
            if (entTinTuc != null)
            {
                TinTuc entTinTucId = DataRepository.TinTucProvider.GetById(entTinTuc.Id);
                lblTomTat.Text = entTinTuc.TomTat;
                littitle.Text = entTinTuc.TieuDe;
                lblTieuDeTin.Text = entTinTuc.TieuDe;
                lblNoiDungTin.Text = Server.HtmlDecode(entTinTuc.NoiDung);

                string testNgay = Common.StandardStringjquery(entTinTuc.NgayCapNhat.ToString("dddd"));
                lblNgayCapNhat.Text = entTinTuc.NgayCapNhat.ToLongDateString();
                //if (testNgay == "Chủ Nhật")
                //    lblNgayCapNhat.Text = Common.StandardStringjquery(entTinTuc.NgayCapNhat.ToString("'' dddd dd/MM/yyyy, HH:mm 'GMT+7'"));
                //else
                //    lblNgayCapNhat.Text = Common.StandardStringjquery(entTinTuc.NgayCapNhat.ToString("'Thứ' dddd dd/MM/yyyy, HH:mm 'GMT+7'"));
            }
        }
        catch (Exception ex)
        { }
    }

    protected void cmdSave_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            CaptchaControl1.ValidateCaptcha(txtCaptcha.Text);
            if (CaptchaControl1.UserValidated)
            {
                BinhLuanTin entBinhLuanTin = new BinhLuanTin();
                entBinhLuanTin.HoTen = Common.StandardString(txtHoTen.Text);
                entBinhLuanTin.Mail = Common.StandardString(txtEmail.Text);
                entBinhLuanTin.TieuDe = Common.StandardString(txtTieuDe.Text);
                entBinhLuanTin.NoiDung = Common.StandardString(txtNoiDung.Text);
                entBinhLuanTin.IdTinTuc = Convert.ToInt32(H_CatalogId.Value);

                entBinhLuanTin.NgayCapNhat = Common.GetServerDateTime();
                entBinhLuanTin.TrangThai = false;

                if (entBinhLuanTin.Id == 0)
                {
                    entBinhLuanTin.Id = 0;
                    updateStatus = DataRepository.BinhLuanTinProvider.Insert(entBinhLuanTin);
                }
                if (updateStatus)
                {
                    cmdReset_Click(sender, e);
                    panelinfor.Visible = false;
                    panelnext.Visible = true;
                }
                else
                    WebMessage.Show("Cập nhật Bình luận thất bại!");
            }
            else
            {
                lblResult.Text = "Bạn đã gõ sai mã bảo vệ";
                lblResult.ForeColor = System.Drawing.Color.Red;
            }
        }
        catch (Exception ex)
        {
            Common.WriteLog("TinTucND.aspx.cs", "cmdSave_Click", ex.ToString());
            WebMessage.Show("Cập nhật Bình luận thất bại!");
        }
    }

    protected void cmdLui_Click(object sender, ImageClickEventArgs e)
    {
        panelinfor.Visible = true;
        panelnext.Visible = false;
    }

    protected void cmdReset_Click(object sender, ImageClickEventArgs e)
    {
        txtEmail.Text = "";
        txtHoTen.Text = "";
        txtNoiDung.Text = "";
        txtTieuDe.Text = "";
    }

    /// <summary>
    /// Hiển thị Bình luận
    /// </summary>
    private void HienThiBinhLuan(int PageNo)
    {
        int TotalRows = 0;
        string WhereClause = "TrangThai= 1 and IdTinTuc=" + H_CatalogId.Value;
        DataSet dsTinTuc = DataRepository.Provider.ExecuteDataSet("BinhLuanTin_GetPaged", WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager1.PageSize);
        dlComment.DataSource = dsTinTuc;
        dlComment.DataBind();
        try
        {
            TotalRows = Convert.ToInt32(dsTinTuc.Tables[1].Rows[0][0]);
        }
        catch (Exception)
        {
        }
        Pager1.ItemCount = TotalRows;
        if (TotalRows <= Pager1.PageSize)
            Pager1.Visible = false;
        else
            Pager1.Visible = true;
    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        Thread.Sleep(2000);
        int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndex;
        HienThiBinhLuan(currnetPageIndex);
    }
}